#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    tcpSocket = new QTcpSocket(this);
    isRunning=false;

    ui->start->setVisible(false);
    ui->big->setVisible(false);
    ui->small->setVisible(false);

    scale=1.0;

    connect(tcpSocket,&QTcpSocket::connected,[=](){
        cout <<"connect phone successfully!"<<endl;
    });

    connect(tcpSocket,&QTcpSocket::readyRead,[=](){
        QByteArray buf = tcpSocket->readAll();
//        cout <<"recv:"<<buf.size()<<endl;

        int start=0;
        int index=0;
        char *data=buf.data();
        while(1){
            index=buf.indexOf("#*#",start);
            if(index==-1)
            {
                QByteArray temp(&data[start],buf.size()-start);
                recvMess(temp);
                break;
            }
            else
            {
                QByteArray temp(&data[start],index-start);
                recvMess(temp);
            }
            start=index+3;
        }
    });

    for(int i=0;i<MAX_PIC;i++)
        recvBuf[i]=QByteArray(0,'c');
    recvPlace=0;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::recvMess(QByteArray buf)
{
    char *data=buf.data();
    if(cmp_str(data,"size:"))
    {
        pw=100,ph=100;
        sscanf(&data[5],"%d,%d,%lf",&pw,&ph,&image_scale);
        cout <<pw<<" "<<ph<<" "<<image_scale<<endl;

        char temp[100]="first";
        tcpSocket->write(temp,10);

        ui->label->setText(QString::number(pw)+tr(",")+QString::number(ph)+tr(",")+QString::number(image_scale));

    }
    else if(cmp_str(data,"img"))
    {
        int size=0;
        sscanf(&data[3],"%d",&size);
        recvBuf[recvPlace].clear();
        time.start();
    }
    else if(cmp_str(data,"endImg"))
    {
        pic=QImage((uchar *)recvBuf[recvPlace].data(),pw,ph,QImage::Format_RGB32);
        showPic(pic);

        recvPlace=(recvPlace+1)%MAX_PIC;

        char temp[100]="start";
        tcpSocket->write(temp,10);

        ui->label->setText(QString::number(time.elapsed())+tr("ms"));
    }
    else if(cmp_str(data,"endData"))
    {
        QByteArray arr=qUncompress(recvBuf[recvPlace]);
        QImage temp(pic);
        uchar *last=temp.bits();
        uchar *data1=(uchar *)arr.data();
        int size=arr.size();
        for(int i=0;i<size;i++)
        {
            last[i]=last[i]+data1[i];
        }
        showPic(temp);
        pic=QImage(temp);
        recvPlace=(recvPlace+1)%MAX_PIC;

        ui->label->setText(QString::number(time.elapsed())+tr("ms"));
    }
    else
    {
        recvBuf[recvPlace].append(buf);
//        cout <<"data:"<<buf.size()<<endl;
    }
}

void MainWindow::showPic(QImage img)
{
    QImage temp=img.scaled(img.width()*scale,img.height()*scale);
    ui->ImgLabel->resize(temp.width(),temp.height());
    ui->ImgLabel->setPixmap(QPixmap::fromImage(temp));

}


void MainWindow::on_start_clicked()
{
    if(!isRunning)
    {
        char temp[100]="size";
        tcpSocket->write(temp,10);
        isRunning=true;
        ui->start->setText(tr("close"));
    }
    else
    {
        char temp[100]="stop";
        tcpSocket->write(temp,10);
        isRunning=false;
        ui->start->setText(tr("start"));
    }
}

void MainWindow::on_btn_clicked()
{
    QHostAddress test;
    QString ip=ui->input->text();
    if (test.setAddress(ip))
    {
        tcpSocket->connectToHost(QHostAddress(ip),8543);
        if(tcpSocket->isOpen())
        {
            ui->input->setVisible(false);
            ui->btn->setVisible(false);
            ui->start->setVisible(true);
            ui->big->setVisible(true);
            ui->small->setVisible(true);
            ui->label->setText(tr(""));
        }
        else
        {
            ui->input->setText(tr("connect failed!"));
        }
    }
    else
        ui->input->setText(tr("ip address is wrong!"));

    ui->label->setText(tr("input complete!"));
    QImage temp(500,500,QImage::Format_RGB32);

    uchar *data=temp.bits();
    for(int i=0;i<temp.byteCount();i++)
        data[i]=(i*3)%255;

    ui->ImgLabel->setPixmap(QPixmap::fromImage(temp));
}

void MainWindow::on_input_returnPressed()
{
//    on_btn_clicked();
//    ui->label->setText(tr("input complete!"));

//    QImage temp(500,500,QImage::Format_RGB32);

//    uchar *data=temp.bits();
//    for(int i=0;i<temp.byteCount();i++)
//        data[i]=(i*3)%255;

//    ui->ImgLabel->setPixmap(QPixmap::fromImage(temp));
}


void MainWindow::on_big_clicked()
{
    scale+=0.1;
}

void MainWindow::on_small_clicked()
{
    scale-=0.1;
    if(scale<0.1)
        scale=0.1;
}
